<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="考试ID" prop="examId">
        <el-input v-model="formData.examId" placeholder="请输入考试ID" readonly />
      </el-form-item>
      <!-- 隐藏特控线名称、线类型、科目类型 -->
      <!-- <el-form-item label="特控线名称" prop="lineName">
        <el-input v-model="formData.lineName" placeholder="请输入特控线名称" />
      </el-form-item>
      <el-form-item label="线类型" prop="lineType">
        <el-select v-model="formData.lineType" placeholder="请选择线类型" clearable>
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.EXAM_LINE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="科目类型" prop="subjectType">
        <el-select v-model="formData.subjectType" placeholder="请选择科目类型" clearable>
          <el-option label="物理类" value="physics" />
          <el-option label="历史类" value="history" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="对应省份" prop="province">
        <el-input v-model="formData.province" placeholder="安徽" readonly />
      </el-form-item>
      <el-form-item label="对应年份" prop="year">
        <el-input v-model="formData.year" placeholder="2025" readonly />
      </el-form-item>
      
      <!-- 分数线设置 -->
      <el-divider content-position="left">分数线设置</el-divider>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="物理特控线" prop="physicsScoreLine">
            <el-input v-model="formData.physicsScoreLine" placeholder="请输入物理类特控线" type="number" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="历史特控线" prop="historyScoreLine">
            <el-input v-model="formData.historyScoreLine" placeholder="请输入历史类特控线" type="number" />
          </el-form-item>
        </el-col>
      </el-row>
      
      <!-- 隐藏其他设置分组 -->
      <!-- <el-divider content-position="left">其他设置</el-divider>
        <el-form-item label="分数线" prop="scoreLine">
          <el-input v-model="formData.scoreLine" placeholder="请输入分数线" type="number" />
        </el-form-item>
        <el-form-item label="对应省控线" prop="provinceControlLine">
          <el-input v-model="formData.provinceControlLine" placeholder="请输入对应省控线" type="number" />
        </el-form-item>
        <el-form-item label="换算比例" prop="conversionRatio">
          <el-input v-model="formData.conversionRatio" placeholder="请输入换算比例(%)" type="number" />
        </el-form-item> -->
      <!-- 隐藏说明字段 -->
       <!-- <el-form-item label="说明" prop="description">
         <Editor v-model="formData.description" height="150px" />
       </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { ExamControlLineApi, ExamControlLineVO } from '@/api/system/examcontrolline'

/** 特控线配置 表单 */
defineOptions({ name: 'ExamControlLineForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  examId: undefined,
  lineName: undefined,
  lineType: undefined,
  scoreLine: undefined,
  conversionRatio: undefined,
  province: undefined,
  year: undefined,
  description: undefined,
  physicsScoreLine: undefined,
  historyScoreLine: undefined,
  physicsProvinceControlLine: undefined,
  historyProvinceControlLine: undefined,
  subjectType: undefined,
  provinceControlLine: undefined,
})
const formRules = reactive({
  examId: [{ required: true, message: '考试ID不能为空', trigger: 'blur' }],
  // 隐藏字段的验证规则
  // lineName: [{ required: true, message: '特控线名称不能为空', trigger: 'blur' }],
  // lineType: [{ required: true, message: '线类型不能为空', trigger: 'change' }],
  // subjectType: [{ required: true, message: '科目类型不能为空', trigger: 'change' }],
  // 分数线设置的两个字段改为必填
  physicsScoreLine: [{ required: true, message: '物理类特控线不能为空', trigger: 'blur' }],
  historyScoreLine: [{ required: true, message: '历史类特控线不能为空', trigger: 'blur' }],
  // 移除省控线字段的验证
  // physicsProvinceControlLine: [{ required: true, message: '物理类省控线不能为空', trigger: 'blur' }],
  // historyProvinceControlLine: [{ required: true, message: '历史类省控线不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number, examId?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 新增时，设置考试ID和默认值
  if (examId && type === 'create') {
    formData.value.examId = examId
    formData.value.province = '安徽'
    formData.value.year = 2025
    // 设置默认的隐藏字段值
    formData.value.lineName = '特控线'
    formData.value.lineType = 'special_control'
    formData.value.subjectType = 'physics'
  }
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ExamControlLineApi.getExamControlLine(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ExamControlLineVO
    if (formType.value === 'create') {
      await ExamControlLineApi.createExamControlLine(data)
      message.success(t('common.createSuccess'))
    } else {
      await ExamControlLineApi.updateExamControlLine(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    examId: undefined,
    lineName: undefined,
    lineType: undefined,
    scoreLine: undefined,
    conversionRatio: undefined,
    province: undefined,
    year: undefined,
    description: undefined,
    physicsScoreLine: undefined,
    historyScoreLine: undefined,
    physicsProvinceControlLine: undefined,
    historyProvinceControlLine: undefined,
    subjectType: undefined,
    provinceControlLine: undefined,
  }
  formRef.value?.resetFields()
}
</script>
